/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.EJB.sessionbeans;

import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import trefliq.EJB.entities.Credential;
import trefliq.EJB.entities.Credit;

/**
 *
 * @author Adam Gojdas
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@PermitAll // (by default, allow all, restrict what's necessary)
public class CreditBean {
    @PersistenceContext(unitName="trefliqPU")
    private EntityManager em;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Long setCreditinmovie(String creditinmovie) {
        Credit instance = new Credit();
        instance.setCreditinmovie(creditinmovie);
        em.persist(instance);
        return instance.getId();
    }
    
    public String getCredits() {
        String allCredits = "";
        List<Credit> credits = em.createNamedQuery("findAllCredits", Credit.class).getResultList();
        for (Credit credit : credits) {
            allCredits += credit.getCreditinmovie() + ", ";
        }
        
        return allCredits;
    }

    public List<Credit> getAllCredits() {
        return em.createNamedQuery("findAllCredits", Credit.class).getResultList();
    }

//    public void getMovieCreditCollection() {
//        Credit instance = new Credit();
//        Collection expResult = null;
//        Collection result = instance.getMovieCreditCollection();
//        result.
//        assertEquals(expResult, result);
//        // TODO review the generated test code and remove the default call to fail.
//        fail("The test case is a prototype.");
//    }
    public Credit CreateCreditinmovie(Credit instance) {
        em.persist(instance);
        return instance;
    }


}
